import Vue from 'vue'
import * as Util from '../components/Util'

let FileService = {
  install (Vue, options) {
    // 给vue增添对话框显示方法
    Vue.FileService = Vue.prototype.$FileService = FileService
  },

  fileSave (data) {
    // 将用户基本信息与最后传值合并
    let result = {}
    if (data.baseinfo.base.f_userinfo_id) {
      result.f_userinfo_id = data.baseinfo.base.f_userinfo_id // 用户id
    }
    if (data.baseinfo.base.version) {
      result.version = data.baseinfo.base.version // 版本号
    }
    result.f_user_type = data.baseinfo.base.f_user_type.length > 0 ? data.baseinfo.base.f_user_type[0] : '' // 用户类型
    result.f_cost_type = data.baseinfo.base.f_cost_type.length > 0 ? data.baseinfo.base.f_cost_type[0] : '' // 缴费类型
    result.f_waterproperties = data.baseinfo.base.f_waterproperties.length > 0 ? data.baseinfo.base.f_waterproperties[0] : '' // 用水性质
    result.f_credentials = data.baseinfo.base.f_credentials.length > 0 ? data.baseinfo.base.f_credentials[0] : '' // 证件类型
    result.f_paper_type = data.baseinfo.base.f_paper_type.length > 0 ? data.baseinfo.base.f_paper_type[0] : '' // 票据类型
    result.f_user_state = data.baseinfo.base.f_user_state.length > 0 ? data.baseinfo.base.f_user_state[0] : '正常' // 用户状态
    result.f_unit = data.baseinfo.base.f_unit1 ? data.baseinfo.base.f_unit1 : data.baseinfo.base.f_unit// 单元号
    result.f_unit_id = data.baseinfo.base.f_unit_id
    result.f_user_name = data.baseinfo.base.f_user_name
    result.f_address = data.baseinfo.base.f_address
    result.f_area = data.baseinfo.base.f_area
    result.f_street = data.baseinfo.base.f_street
    result.f_residential_area = data.baseinfo.base.f_residential_area
    result.f_building = data.baseinfo.base.f_building
    result.f_floor = data.baseinfo.base.f_floor
    result.f_room = data.baseinfo.base.f_room
    result.f_idnumber = data.baseinfo.base.f_idnumber
    result.f_user_phone = data.baseinfo.base.f_user_phone
    result.f_population = data.baseinfo.base.f_population
    result.f_contract_id = data.baseinfo.base.f_contract_id
    result.f_taxpayer_id = data.baseinfo.base.f_taxpayer_id
    result.f_address_phone = data.baseinfo.base.f_address_phone
    result.f_paper_name = data.baseinfo.base.f_paper_name
    result.f_paper_account = data.baseinfo.base.f_paper_account
    result.f_comments = data.baseinfo.base.f_comments
    result.f_taxpayer_id = data.baseinfo.base.f_taxpayer_id
    result.f_address_phone = data.baseinfo.base.f_address_phone
    result.f_paper_name = data.baseinfo.base.f_paper_name
    result.f_paper_account = data.baseinfo.base.f_paper_account
    result.f_comments = data.baseinfo.base.f_comments
    result.f_operator = Util.f.name
    result.f_filiale = Util.f.f_fengongsi
    result.f_outlets = Util.f.f_parentname
    result.f_meterbook_id = data.baseinfo.base.f_meterbook_id
    // 缴费类型为银行代扣时将银行信息合并
    if (data.baseinfo.base.f_cost_type[0] === '银行代扣') {
      result.infobank = {}
      result.infobank.f_bank_accopen = data.baseinfo.bank.f_bank_accopen
      result.infobank.f_bank_name = data.baseinfo.bank.f_bank_name
      result.infobank.f_bank_account = data.baseinfo.bank.f_bank_account
    }
    // 如果是单位建档，没有小区，楼栋，单元，楼层，房间号
    if (data.baseinfo.base.f_user_type[0] === '非民用') {
      data.baseinfo.base.f_residential_area = ''
      data.baseinfo.base.f_building = ''
      data.baseinfo.base.f_unit = ''
      data.baseinfo.base.f_floor = ''
      data.baseinfo.base.f_room = ''
    }
    result.userfiles = []
    // 表具信息
    data.meterinfo.forEach((item, index) => {
      let meter = {}
      if (item.f_userinfo_id) {
        meter.f_user_id = item.f_user_id // 表id
      }
      if (item.version) {
        meter.version = item.version // 版本号
      }
      meter.f_meterbook_number = item.f_meterbook_number
      meter.f_meter_locatid = `${item.f_meterbook_id ? item.f_meterbook_id + '-': ''}${data.baseinfo.base.f_floor ? data.baseinfo.base.f_floor + '-' : ''}${data.baseinfo.base.f_room + '-' ? data.baseinfo.base.f_room : ''}`
      meter.f_position = item.f_position? item.f_position : '' // 安装位置
      meter.f_meter_population = item.f_meter_population// 表内人口
      meter.f_meter_base = item.f_meter_base // 表底数
      meter.f_meternumber = item.f_meternumber // 表号
      meter.f_coefficient = item.f_coefficient// 表系数
      meter.f_metertitles = item.f_metertitles // 表封号
      meter.f_reverse_state = item.f_reverse_state // 反转状态
      meter.f_garbage_type = item.f_garbage_type.length > 0 ? item.f_garbage_type[0] : '' // 垃圾费方案
      meter.f_reverse_state = item.f_reverse_state.length > 0 ? item.f_reverse_state[0] : ''
      meter.f_meterbook_id = item.f_meterbook_id// 表册号(小区编号+楼号)
      meter.f_meter_type = '子表' // 水表类型
      meter.f_meter_classify = item.waterbrand.length > 0 ? item.waterbrand[0].f_meter_type : '' // 水表类型
      meter.f_user_type = data.baseinfo.base.f_user_type.length > 0 ? data.baseinfo.base.f_user_type[0] : ''
      meter.f_waterproperties = data.baseinfo.base.f_waterproperties.length > 0 ? data.baseinfo.base.f_waterproperties[0] : ''
      meter.f_waterbrand_id = item.waterbrand.length > 0 ? item.waterbrand[0].id : ''
      meter.f_watermodel_id = item.waterbrand.length > 0 ? item.watermodel[0].id : ''
      meter.f_inputtor = item.f_inputtor.length > 0 ? item.f_inputtor[0] : '' // 抄表员
      meter.f_install_person = item.f_install_person.length > 0 ? item.f_install_person[0] : '' // 安装人
      meter.f_install_date = item.f_install_date // 安装日期
      meter.f_collector = item.f_collector.length > 0 ? item.f_collector[0] : '' // 催收员
      meter.f_hand_cycle = item.f_hand_cycle.length > 0 ? item.f_hand_cycle[0] : '' // 抄表周期
      meter.f_hand_date = item.f_hand_date
      meter.f_protocol_water = item.f_protocol_water // 协议水量
      meter.f_rent_name = item.f_rent_name
      meter.f_rent_phone = item.f_rent_phone // 协议水量
      meter.f_filiale = Util.f.f_fengongsi
      meter.f_outlets = Util.f.f_parentname
      meter.f_operator = Util.f.name
      meter.f_valve_state = '开阀'
      meter.f_note = item.f_note ? item.f_note : '' // 备注
      if (item.userprice.userprices.length > 0) {
        meter.userprice = []
        item.userprice.userprices.forEach((item1) => {
          let uprice = {
            f_price_name: item1.f_price_name,
            f_price_id: item1.f_price_id,
            f_fixed_water: item1.f_fixed_water,
            f_ratio: item1.f_ratio / 100,
            f_describe: item1.f_describe,
            f_state: item1.f_state,
          }
          if (item1.id) {
            uprice.id = item1.id
          }
          meter.userprice.push(uprice)
        })
      }
      if (item.freeprice.freeprices.length > 0) {
        meter.freeprice = []
        item.freeprice.freeprices.forEach((item1) => {
          let fprice = {
            f_price_name: item1.f_price_name,
            f_free_type: item1.f_free_type,
            f_free_item: item1.f_free_item,
            f_free_water: item1.f_free_water,
            f_free_proport: item1.f_free_proport / 100,
            f_waterproperties: result.f_waterproperties.length > 0 ? data.baseinfo.base.f_waterproperties[0] : '',
            f_start_date: item1.f_start_date,
            f_state: item1.f_state,
            f_finish_date: item1.f_finish_date,
            f_filiale: Util.f.f_fengongsi,
            f_outlets: Util.f.f_parentname,
            f_operator: Util.f.name
          }
          if (item1.id) {
            fprice.id = item1.id
          }
          meter.freeprice.push(fprice)
        })
      }
      if (!item.f_user_id) {
        meter.f_table_state = '正常'
        meter.f_whether_hairpin = '未发'
        meter.f_aroundmeter = '左表'
      }
      result.userfiles.push(meter)
    })
    return Vue.resetpost('rs/logic/fileSave', {data: {userinfo: result}})
  },
  batchOperate (model, type) {
    let data = {
      f_operat_type: '销户',
      f_describe: `对用户${model.userinfo.f_user_name}进行销户`,
      f_state: '有效',
      f_user_id: model.userinfo.f_user_id,
      record_userinfo: model.userinfo.f_userinfo_id,
      f_user_name: model.userinfo.f_user_name,
      f_cancel_state: type,
      f_cancel_cause: model.f_reason,
      f_operator: Util.f.name,
      f_filiale: Util.f.f_fengongsi,
      f_outlets: Util.f.f_parentname
    }
    if (type === '销户') {
      data.userinfo = {
        f_userinfo_id: model.userinfo.f_userinfo_id,
        f_user_state: '销户',
        version: model.userinfo.version
      }
    } else if (type === '启用') {
      data.f_describe = `对用户${model.userinfo.f_user_name}进行启用`
      data.userinfo = {
        f_userinfo_id: model.userinfo.f_userinfo_id,
        f_user_state: '正常',
        version: model.userinfo.version
      }
    }
    return Vue.resetpost('rs/entity/t_cancellation', data, {resolveMsg: `${type}成功`, rejectMsg: `${type}失败`})
  }
}

export default FileService
